package com.sangmo.fts.process.inventory.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.fts.basic.model.FileUpload;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.process.inventory.model.IfStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class IfStockDocumentService {

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private IfStockEntityService ifStockEntityService;

    public FileUpload createConfirmDoc(Long stockId, Long fileUploadId) {
        IfStock stock = ifStockEntityService.get(stockId);
        if (IfStock.States.CHECKED.equals(stock.getState())) {
            throw new ServiceException(StandardErrors.CLIENT_ERROR.getStatus(), "error.ifStock.upload.permit.disabled");
        }
        stock.setConfirmDoc(fileUploadId);
        if (IfStock.States.INBOUND.equals(stock.getState())) {
            stock.setState(IfStock.States.PENDING);
        }
        ifStockEntityService.save(stock);
        return fileUploadService.updateRef(fileUploadId, ""+stockId, "IfStock");
    }

}
